/**
 * Package:com.afteryuan.server.hanlder.core;
 * $Id$
 * Copyright(c) 2001-2005 www.afteryuan.com
 */
package com.afteryuan.server.handler.core;

import com.afteryuan.server.ConnectSession;
import com.afteryuan.server.command.down.core.ExceptionDown;
import com.afteryuan.server.command.down.core.JoinRoomDown;
import com.afteryuan.server.command.down.core.MessageDown;
import com.afteryuan.server.command.down.core.MoveAvatarDown;
import com.afteryuan.server.command.model.Avatar;
import com.afteryuan.server.command.up.CommandUp;
import com.afteryuan.server.command.up.core.JoinRoomUp;
import com.afteryuan.server.config.Configuration;
import com.afteryuan.server.data.Room;
import com.afteryuan.server.data.Sprite;
import com.afteryuan.server.data.SpritePool;
import com.afteryuan.server.data.SpriteUtil;
import com.afteryuan.server.handler.Handler;
import com.afteryuan.server.service.SpriteService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.*;

/**
 * JoinRoomHandler 处理加入Room的请求
 * <p/>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author$</a>
 * @version $Revision$
 * @see com.afteryuan.server.command.down.core.JoinRoomDown
 * @see com.afteryuan.server.command.down.core.MoveAvatarDown
 *      <p><a href="PingHandler.java.html"><i>View Source</i></a></p>
 */
public class JoinRoomHandler implements Handler {
    private static Log log = LogFactory.getLog(JoinRoomHandler.class);
    private SpritePool spritePool;
    private SpriteService spriteService;

    public void setSpritePool(SpritePool spritePool) {
        this.spritePool = spritePool;
    }

    public void setSpriteService(SpriteService spriteService) {
        this.spriteService = spriteService;
    }

    /**
     * 处理步骤:<br/>
     * 1.检查用户是否登录,如果没有,则返回异常<br/>
     * 2.将其加入Room<br/>
     * 3.发送场景内其它用户信息给当前用户.<br/>
     * 4.通知场景内其他avatar<br/>
     * 5.全系统广播avatar登入的消息(可配置)<br/>
     *
     * @param session 用户session
     * @param command 上行命令
     */
    public void process(ConnectSession session, CommandUp command) {
        log.debug("do JoinRoom");

        JoinRoomUp cmd = (JoinRoomUp) command;
        //1.检查用户是否登录,如果没有,则返回异常
        Sprite sprite = spritePool.get(cmd.sender);
        if (sprite == null) {
            session.write(new ExceptionDown(ExceptionDown.AVATAR_NOT_EXIST));
            return;
        }

        Room room = new Room(cmd.sceneId, cmd.sceneInstanceId);
        Collection<Sprite> sprites = spritePool.listOfRoom(room);

        //2.将其加入Room
        sprite.setRoom(room);
        spritePool.addToRoom(sprite);

        //3.发送场景内其它用户信息给当前用户.
        JoinRoomDown joinRoomDown = new JoinRoomDown();
        List<Avatar> avatars = new ArrayList<Avatar>();
        if (sprites != null) {
            for (Sprite s : sprites) {

                avatars.add(SpriteUtil.buildEnterAvatar(s));
            }
        }
        joinRoomDown.avatars = avatars;
        session.write(joinRoomDown);

        //4.通知场景内其他avatar
        if (sprites != null) {
            for (Sprite s : sprites) {
                s.getConnectSession().write(new MoveAvatarDown(SpriteUtil.buildEnterAvatar(sprite)));
            }
        }

        //5.全系统广播avatar登入的消息(可配置)
        if (Configuration.getInstance().isBroadcastJoinRoom()) {
            MessageDown m = new MessageDown();
            m.code = MessageDown.CODE_JOINROOM;
            m.scope = MessageDown.SCOPE_SERVER;
            Map<String, String> h = new HashMap<String, String>();
            h.put("avatarId", sprite.getId());
            m.headers = h;
            spriteService.broadcast(m);
        }
    }
}